package com.ideal.manage.esms.service.monitor;

import com.alibaba.fastjson.JSONObject;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval;
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder;

public class ChartUtils {
    public static class AggregationCondition {
        public String fmt;
        public JSONObject defaultData;
        public DateHistogramInterval interval;

        public AggregationCondition(String fmt, JSONObject defaultData, DateHistogramInterval interval) {
            this.fmt = fmt;
            this.defaultData = defaultData;
            this.interval = interval;
        }
    }

    public static ESValueSourceAggregation getAggregation(String showType) {
        switch (showType) {
            case "avg":
                return new ESValueSourceAggregation("avg");
            case "min":
                return new ESValueSourceAggregation("min");
            case "max":
                return new ESValueSourceAggregation("max");
            case "now":
                return new ESValueSourceAggregation("now");
            default:
                return null;
        }
    }

    public static AggregationCondition buildCondition(String frequency, String ranges) {
        switch (frequency) {
            case "15":
                return new AggregationCondition("dd HH:mm", Utils.getFifteenMinutes(),
                        DateHistogramInterval.minutes(15));
            case "hour":
                return new AggregationCondition("yyy-MM-dd HH", Utils.getHour(), DateHistogramInterval.HOUR);
            case "day":
                return new AggregationCondition("yyy-MM-dd", Utils.getDay(ranges), DateHistogramInterval.DAY);
            case "month":
                return new AggregationCondition("yyyy-MM", Utils.getMonth(), DateHistogramInterval.MONTH);
            default:
                return null;
        }
    }


}
